package net.oschina.gpg.controller;

import net.oschina.gpg.bo.Question;
import net.oschina.gpg.dao.QuestionDao;
import net.oschina.gpg.util.GenenrateCaptchaUtil;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOError;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Created with bluescreen
 *
 * @author ; gpg(gpg512@sina.cn)
 * DATE:2016/10/19
 */
@Controller
public class IndexController {

    @Autowired
    QuestionDao questionDao;

    @RequestMapping("/list")
    public String list(Map<String, Object> map){
        map.put("questions", testQuestions());
        return "list";
    }

    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException{
        GenenrateCaptchaUtil.genenrateCaptcha(request, response);
    }


    private List<Question> testQuestions(){
        List<Question> questions = new ArrayList<>();
        for (int i = 0 ; i < 5;i++){
            Question question = new Question();
            question.setId(i+1);
            question.setqTitle(ThreadLocalRandom.current().nextInt(100)+ "丰富的三分得手分斯蒂芬斯蒂芬森发生的 "+ ThreadLocalRandom.current().nextInt(100));
            question.setqCreateTime(new Date());
            questions.add(question);
        }

        return questions;
    }
}
